package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.AllUser;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.AnimationUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：Administrator on 2017/8/9 18:30
 * 邮箱：zhanghuaiha@gmail.com
 */

public class MyExpandableListView extends BaseExpandableListAdapter {

    private List<String> pList = new ArrayList<>();
    private Map<Integer,List<AllUser>> cList = new HashMap<>();
    private Context mContext;
    private boolean flag = true;
    public MyExpandableListView(Context context,List<String> pList, Map<Integer, List<AllUser>> cList){
        this.pList = pList;
        this.cList = cList;
        mContext = context;
    }

    //返回一级列表的个数
    @Override
    public int getGroupCount() {
        return pList.size();
    }

    //返回二级列表的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return cList.get(groupPosition).size();
    }

    //返回一级列表的单个item（返回的是对象）
    @Override
    public Object getGroup(int groupPosition) {
        return pList.get(groupPosition);
    }

    //返回二级列表中的单个item（返回的是对象）
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return cList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //每个item的id是否是固定？一般为true
    @Override
    public boolean hasStableIds() {
        return true;
    }

    //【重要】填充一级列表
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grouping_parent, null);
            viewHolder = new ViewHolder(convertView);
            viewHolder.tv_parent = (TextView) convertView.findViewById(R.id.tv_parent);
            viewHolder.xia = (ImageView) convertView.findViewById(R.id.xia);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        viewHolder.tv_parent.setText(pList.get(groupPosition));
        return convertView;
    }
    //【重要】填充二级列表
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_grouping_child, null);
            viewHolder = new ChildViewHolder(convertView);
            viewHolder.tv_child = (TextView) convertView.findViewById(R.id.tv_child);
            viewHolder.tel_child = (TextView) convertView.findViewById(R.id.tel_child);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }
        System.out.println("groupPosition:"+groupPosition+"-----childPosition:"+childPosition);
        viewHolder.tv_child.setText(cList.get(groupPosition).get(childPosition).getName());
        viewHolder.tel_child.setText(cList.get(groupPosition).get(childPosition).getMobile());
        return convertView;
    }
    //二级列表中的item是否能够被选中？可以改为true
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ViewHolder {

        TextView tv_parent;
        ImageView xia;

        public ViewHolder(View view) {
            tv_parent = (TextView) view.findViewById(R.id.tv_parent);
            xia = (ImageView) view.findViewById(R.id.xia);
        }
    }

    private class ChildViewHolder {

        TextView tel_child;
        TextView tv_child;

        public ChildViewHolder(View view) {
            tel_child = (TextView) view.findViewById(R.id.tel_child);
            tv_child = (TextView) view.findViewById(R.id.tv_child);
        }
    }
}
